const PositionSql = require('../../models/position')
const ResHandler = require("../../utils/ResHandler")
const moment = require('moment'); 
const positionSql = new PositionSql()
const resHandler = new ResHandler()
class PositionContr {
  async findPostion(req, res, next) {
    try {
      const positionList = await positionSql.getPostions()
      res.json(resHandler.succRes({ data: positionList }))
    } catch(err) {
      next(err)
    }
  }
  async addPosition(req, res, next) {
    try {
      const data = {
        ...req.body,
        createTime: moment().format("YYYY-MM-DD,h:mm:ss a")
      }
      const result = await positionSql.addPostion(data)
      if(result._id) {
        res.json(resHandler.succRes({ message: '添加成功' }))
      } else {
        res.json(resHandler.errRes({ message: '操作失败' }))
      }
    } catch(err) {
      next(err)
    }
  }
  async deletePosition(req, res, next) {
    try {
      const data = req.body
      try {
        for (let i = 0; i < data.length; i++) {
          const result = await positionSql.deletePosition(data[i])
          console.log(result, i);
        }
        res.json(resHandler.succRes({ message: '删除成功' }))
      } catch(err) {
        console.log(err,'errcc');
        next(err);
      }
    } catch(err) {
      next(err)
    }
  }
}

module.exports = PositionContr